﻿using System;

namespace cz.zcu.PreSti.Components
{
    public class ExponentialDistributionGenerator : BaseComponent
    {
        private Random r;
        private double variance = 1;
        private double cropSize = 0;
        private double translation = 0;
        #region properties

        public double Variance
        {
            set
            {
                variance = value;
            }
            get
            {
                return variance;
            }
        }

        public double CropSize
        {
            set
            {
                cropSize = value;
            }
            get
            {
                return cropSize;
            }
        }

        public double Translation
        {
            set
            {
                translation = value;
            }
            get
            {
                return translation;
            }
        }

        #endregion

        public ExponentialDistributionGenerator() : base(0, 1)
        {
            r = new Random(this.GetHashCode()); //bug 6911: constructor without parameter makes all generators not really random
        }

        public override object getValueFromGetter(int getterN)
        {
            switch (getterN)
            {
                case 0:
                    double result;
                    do
                    {
                        result = -Math.Log(1 - r.NextDouble()) / variance;
                    }
                    while (cropSize > 0 && (result > CropSize));
                    result += translation;
                    return result;
            }
            return base.getValueFromGetter(getterN);
        }

    }
}
